package com.tarena.lbs.marketing.web.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.marketing.web.mapper.UserCouponsMapper;
import com.tarena.lbs.pojo.marketing.po.UserCouponsPO;
import com.tarena.lbs.pojo.marketing.query.UserCouponQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserCouponsRepository {
    @Autowired
    private UserCouponsMapper userCouponsMapper;

    public Long countUserCoupons(Integer couponId, Integer userId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("coupon_id",couponId);
        queryWrapper.eq("user_id",userId);
        return userCouponsMapper.selectCount(queryWrapper);
    }

    public void saveUserCoupons(UserCouponsPO poParam) {
        userCouponsMapper.insert(poParam);
    }

    public PageInfo<UserCouponsPO> userCouponsPage(UserCouponQuery query) {
        QueryWrapper queryWrapper=new QueryWrapper();
        if (query.getUserId()!=null){
            queryWrapper.eq("user_id",query.getUserId());
        }
        if (query.getCouponType()!=null){
            queryWrapper.eq("coupon_type",query.getCouponType());
        }
        if (query.getStatus()!=null){
            queryWrapper.eq("status",query.getStatus());
        }
        if (query.getOrderNo()!=null){
            queryWrapper.eq("order_no",query.getOrderNo());
        }
        if (query.getReceiveChannel()!=null){
            queryWrapper.eq("receive_channel",query.getReceiveChannel());
        }
        queryWrapper.orderByDesc("create_at");
        PageHelper.startPage(query.getPageNo(),query.getPageSize());
        List<UserCouponsPO> pos= userCouponsMapper.selectList(queryWrapper);
        PageInfo<UserCouponsPO> pageInfo=new PageInfo<>(pos);
        return pageInfo;
    }

    public UserCouponsPO getUserCouponsDetail(String couponCode) {
        //select * from user_coupons where coupon_code=#{code}
        QueryWrapper<UserCouponsPO> queryWrapper=new QueryWrapper();
        queryWrapper.eq("coupon_code",couponCode);
        return userCouponsMapper.selectOne(queryWrapper);
    }
}
